package com.ysm.smfu.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.ysm.smfu.R;
import com.ysm.smfu.bean.HomeProduct;
import com.ysm.smfu.fragment.BendiFragment;
import com.ysm.smfu.fragment.HomeFragment;
import com.ysm.smfu.holder.BaseHolder;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.RecyclerImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/17.
 */
public class HomeProductHolder extends BaseHolder<HomeProduct.Goods> {

    private Object object;
    private View view;
    @BindView(R.id.home_list_item_img)
    RecyclerImageView homeListItemImg;
    @BindView(R.id.home_list_item_title)
    TextView homeListItemTitle;
    @BindView(R.id.home_list_item_profile)
    TextView homeListItemProfile;
    @BindView(R.id.home_list_item_location)
    TextView homeListItemLocation;
    @BindView(R.id.home_list_item_distance)
    TextView homeListItemDistance;

    public HomeProductHolder(Object object) {
        this.object = object;
    }

    @Override
    protected View initView() {
        view = UIUtils.inflate(R.layout.home_listview_item);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void refreshView(HomeProduct.Goods data) {
        if (!TextUtils.isEmpty(data.thumb) && !data.thumb.startsWith("null?")) {
            loadImageWithUrl(data, object);
        }
        homeListItemTitle.setText(data.title);
        if (0 < data.discount && data.discount < 10)
            homeListItemProfile.setText("折扣:" + (data.discount));
        homeListItemLocation.setText(data.price);
        homeListItemDistance.setVisibility(View.GONE);
    }

    /**
     * 从网络加载图片
     */
    private void loadImageWithUrl(HomeProduct.Goods goods, Object object) {
        RequestManager requestManager = null;
        if (object instanceof HomeFragment) {
            requestManager = Glide.with((HomeFragment) object);
        } else if (object instanceof BendiFragment)
            requestManager = Glide.with((BendiFragment) object);
        if (requestManager != null)
            requestManager.load(goods.thumb)
                    .placeholder(R.drawable.pay_businessicon_defualt)
                    .error(R.drawable.pay_businessicon_defualt)
                    .override(140, 140)
//            显示缩略图
                    .thumbnail(0.1f)
                    .centerCrop()
                    .into(homeListItemImg);
    }

}
